package com.fr.schedule.dao;

import com.fr.base.FRContext;
import com.fr.base.ReflectionUtils;
import com.fr.base.StringUtils;
import com.fr.base.dao.JdbcTemplate;
import com.fr.base.dao.ResultSetExtractor;
import com.fr.base.file.DatasourceManager;
import com.fr.data.core.db.dialect.DialectFactory;
import com.fr.data.core.db.dialect.OracleDialect;
import com.fr.data.impl.DatabaseConnection;
import com.fr.data.impl.JDBCDatabaseConnection;
import com.fr.schedule.output.EmailNotification;
import com.fr.schedule.output.ScheduledOutput;
import com.fr.schedule.task.ScheduleTask;
import com.fr.schedule.trigger.CalendarITrigger;
import com.fr.schedule.trigger.OnceITrigger;
import com.fr.schedule.trigger.SimpleITrigger;
import com.fr.web.platform.entry.FileEntry;
import com.fr.web.platform.entry.FolderEntry;
import com.fr.web.platform.entry.ReportletEntry;
import com.fr.web.platform.entry.URLEntry;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JdbcDaoManager
{
  private DatabaseConnection databaseConnection;
  private static Map MAPPING = new HashMap();

  public JdbcDaoManager()
  {
    DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
    if (StringUtils.isEmpty(localDatasourceManager.getScheduleDatasource()))
    {
      FRContext.getLogger().log(Level.WARNING, "Can't found the schedule database!");
      return;
    }
    this.databaseConnection = ((DatabaseConnection)localDatasourceManager.getConnection(localDatasourceManager.getScheduleDatasource()));
  }

  public JdbcDaoManager(JDBCDatabaseConnection paramJDBCDatabaseConnection)
  {
    this.databaseConnection = paramJDBCDatabaseConnection;
  }

  public boolean save(Object paramObject)
  {
    Class localClass = paramObject.getClass();
    assertIsInMapping(localClass);
    boolean bool = isOracleDatabaseConnection();
    long l = ((Number)new JdbcTemplate(this.databaseConnection).execute(getObjectMapper(localClass).createInsert(paramObject, bool), true)).longValue();
    return ReflectionUtils.setPrivateFieldValue(paramObject, "id", new Long(l));
  }

  public boolean saveOrUpdate(Object paramObject)
  {
    long l = ((Number)ReflectionUtils.getPrivateFieldValue(paramObject, "id")).longValue();
    if (l < 0L)
      return save(paramObject);
    return update(paramObject);
  }

  public boolean update(Object paramObject)
  {
    Class localClass = paramObject.getClass();
    assertIsInMapping(localClass);
    return new JdbcTemplate(this.databaseConnection).execute(getObjectMapper(localClass).createUpdate(paramObject));
  }

  public Object load(Class paramClass, long paramLong)
  {
    assertIsInMapping(paramClass);
    ObjectMapper localObjectMapper = getObjectMapper(paramClass);
    Object localObject = null;
    try
    {
      localObject = new JdbcTemplate(this.databaseConnection).query(localObjectMapper.createLoadSQL(paramLong), new ResultSetExtractor(this, localObjectMapper, paramClass)
      {
        private final ObjectMapper val$objectMapper;
        private final Class val$clazz;
        private final JdbcDaoManager this$0;

        public Object extractData()
          throws SQLException, Exception
        {
          if (paramResultSet.next())
            return this.val$objectMapper.extractResultSet(paramResultSet, this.val$clazz);
          return null;
        }
      });
    }
    catch (Exception localException)
    {
      throw new RuntimeException(localException);
    }
    return localObject;
  }

  public List listByFieldValue(Class paramClass, String paramString, Object paramObject)
  {
    assertIsInMapping(paramClass);
    ObjectMapper localObjectMapper = getObjectMapper(paramClass);
    Object localObject = new ArrayList();
    try
    {
      localObject = (List)new JdbcTemplate(this.databaseConnection).query(localObjectMapper.createSQLByFiledValue(paramString, paramObject), new ResultSetExtractor(this, localObjectMapper, paramClass)
      {
        private final ObjectMapper val$objectMapper;
        private final Class val$clazz;
        private final JdbcDaoManager this$0;

        public Object extractData()
          throws SQLException, Exception
        {
          ArrayList localArrayList = new ArrayList();
          while (paramResultSet.next())
            localArrayList.add(this.val$objectMapper.extractResultSet(paramResultSet, this.val$clazz));
          return localArrayList;
        }
      });
    }
    catch (Exception localException)
    {
      throw new RuntimeException(localException);
    }
    return ((List)localObject);
  }

  public List list(Class paramClass)
  {
    assertIsInMapping(paramClass);
    ObjectMapper localObjectMapper = getObjectMapper(paramClass);
    Object localObject = new ArrayList();
    try
    {
      localObject = (List)new JdbcTemplate(this.databaseConnection).query(localObjectMapper.createSelectAllSQL(), new ResultSetExtractor(this, localObjectMapper, paramClass)
      {
        private final ObjectMapper val$objectMapper;
        private final Class val$clazz;
        private final JdbcDaoManager this$0;

        public Object extractData()
          throws SQLException, Exception
        {
          ArrayList localArrayList = new ArrayList();
          while (paramResultSet.next())
            localArrayList.add(this.val$objectMapper.extractResultSet(paramResultSet, this.val$clazz));
          return localArrayList;
        }
      });
    }
    catch (Exception localException)
    {
      throw new RuntimeException(localException);
    }
    return ((List)localObject);
  }

  public void deleteById(Class paramClass, long paramLong)
  {
    assertIsInMapping(paramClass);
    ObjectMapper localObjectMapper = getObjectMapper(paramClass);
    try
    {
      new JdbcTemplate(this.databaseConnection).execute(localObjectMapper.createDeleteByIdSQL(paramLong));
    }
    catch (Exception localException)
    {
      throw new RuntimeException(localException);
    }
  }

  public void delete(Class paramClass)
  {
    assertIsInMapping(paramClass);
    ObjectMapper localObjectMapper = getObjectMapper(paramClass);
    try
    {
      new JdbcTemplate(this.databaseConnection).execute(localObjectMapper.createDeleteAllSQL());
    }
    catch (Exception localException)
    {
      throw new RuntimeException(localException);
    }
  }

  public void delete(Object paramObject)
  {
    assertIsInMapping(paramObject.getClass());
    ObjectMapper localObjectMapper = getObjectMapper(paramObject.getClass());
    try
    {
      new JdbcTemplate(this.databaseConnection).execute(localObjectMapper.deleteByObject(paramObject));
    }
    catch (Exception localException)
    {
      throw new RuntimeException(localException);
    }
  }

  private static ObjectMapper getObjectMapper(Class paramClass)
  {
    return ((ObjectMapper)MAPPING.get(paramClass));
  }

  public void setDatabaseConnection(DatabaseConnection paramDatabaseConnection)
  {
    this.databaseConnection = paramDatabaseConnection;
  }

  public DatabaseConnection getDatabaseConnection()
  {
    return this.databaseConnection;
  }

  private static void assertIsInMapping(Class paramClass)
  {
    if (!(MAPPING.containsKey(paramClass)))
      throw new RuntimeException(paramClass + " doesn't exists in mapping");
  }

  private boolean isOracleDatabaseConnection()
  {
    if (this.databaseConnection instanceof JDBCDatabaseConnection)
    {
      localObject = (JDBCDatabaseConnection)this.databaseConnection;
      String str = ((JDBCDatabaseConnection)localObject).getDriver();
      return ((str != null) && (str.endsWith("OracleDriver")));
    }
    Object localObject = null;
    try
    {
      localObject = this.databaseConnection.createConnection();
      return DialectFactory.generateDialect((Connection)localObject) instanceof OracleDialect;
    }
    catch (Exception localException)
    {
      if (localObject != null)
        try
        {
          ((Connection)localObject).close();
        }
        catch (SQLException localSQLException)
        {
          localSQLException.printStackTrace();
        }
    }
    return false;
  }

  static
  {
    MAPPING.put(FolderEntry.class, FolderEntry.OBJECT_MAPPER);
    MAPPING.put(FileEntry.class, FileEntry.OBJECT_MAPPER);
    MAPPING.put(ReportletEntry.class, ReportletEntry.OBJECT_MAPPER);
    MAPPING.put(URLEntry.class, URLEntry.OBJECT_MAPPER);
    MAPPING.put(ScheduleTask.class, ScheduleTask.OBJECT_MAPPER);
    MAPPING.put(ScheduledOutput.class, ScheduledOutput.OBJECT_MAPPER);
    MAPPING.put(EmailNotification.class, EmailNotification.OBJECT_MAPPER);
    MAPPING.put(OnceITrigger.class, OnceITrigger.OBJECT_MAPPER);
    MAPPING.put(SimpleITrigger.class, SimpleITrigger.OBJECT_MAPPER);
    MAPPING.put(CalendarITrigger.class, CalendarITrigger.OBJECT_MAPPER);
  }
}